package org.methodbox.extractor;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 *
 * @author Christian
 */
public class Unpacker1 {

    private BufferedReader input;

    private BufferedWriter[] outWriters;

    private String[] columnNames;

    private int start = 0;

    private int numColumns;

    private void openInput(String inputFileName) throws ExtractorException{
        try {
            File inputFile = new File(inputFileName);
            if (inputFileName.toLowerCase().endsWith(".zip")){
                FileInputStream raw = new FileInputStream(inputFile);
                ZipInputStream zip = new ZipInputStream(raw);
                ZipEntry entry = zip.getNextEntry();
                input = new BufferedReader (new InputStreamReader(zip));
            } else {
                input = new BufferedReader (new FileReader(inputFile));
            }
        } catch (IOException ex) {
            throw new ExtractorException(ex);
        }
    }

    private void openOutput(String outputDirName) throws ExtractorException{
        try {
            File outputDir = new File(outputDirName);
            if (outputDir.exists()){
                if (outputDir.isFile()){
                    throw new ExtractorException(outputDirName + " points to a file.");
                }
            } else {
                outputDir.mkdirs();
            }
            outWriters = new BufferedWriter[numColumns];
            for (int i = 0; i<numColumns; i++){
                File outputFile = new File (outputDir,columnNames[i]+".txt");
                outWriters[i] = new BufferedWriter(new FileWriter(outputFile));
            }
        } catch (IOException ex) {
            throw new ExtractorException(ex);
        //} catch (FileNotFoundException ex) {
        //    throw new ExtractorException(ex);
        }
    }

    private void findColumns() throws ExtractorException{
        try {
            String headLine = input.readLine();
            columnNames = headLine.split("\t");
            numColumns = columnNames.length;
        } catch (IOException ex) {
            throw new ExtractorException(ex);
        }
    }

    private void close() throws ExtractorException{
        try {
            input.close();
            for (int i = 0; i<numColumns; i++){
                outWriters[i].close();
            }
        } catch (IOException ex) {
             throw new ExtractorException(ex);
        }
    }

    public Unpacker1(String inputFileName, String outputDirName)
            throws ExtractorException{
        openInput(inputFileName);
        findColumns();
        openOutput(outputDirName);
    }

    public void run() throws ExtractorException{
        try {
            while (input.ready()) {
                String inputLine = input.readLine();
                String[] inputs = inputLine.split("\t");
                for (int i = 0; i<numColumns; i++){
                    outWriters[i].write(inputs[i+start]+"\n");
                }
            }
        } catch (IOException ex) {
             throw new ExtractorException(ex);
        }
       close();
    }

    public static void main( String[] args ) throws ExtractorException
    {
        //tring inputFileName = "C:/Ruby/elab/java/extractor/Data/small.data";
        //tring columnsToPick = "Afford4,Amthb";
        String inputFileName = "C:/Ruby/elab/java/extractor/Data/large.zip";
        String columnsToPick = "CHILDREN,INFANTS,SRCINC03,YTAKE092,EVERJOB,INDCULT3,SDQCHK,NSSEC8,DRMTH06,HSERIAL,INDCULT3,";
        String exportName = "test";
        String year = "2010";
        String outputDirName = "C:/Ruby/elab/java/extractor/large";
        Unpacker1 parser = new Unpacker1(inputFileName, outputDirName);
        parser.run();
    }

}
